package my.avoskagae.service.local;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

import my.avoskagae.model.AvoskaModelException;
import my.avoskagae.model.buslogic.MaintenanceProductCatalog;
import my.avoskagae.model.entity.Product;
import my.avoskagae.service.abstr.AvoskaServiceAsync;

public class AvoskaServiceLocalAsyncImpl implements AvoskaServiceAsync {

	@Inject
	protected MaintenanceProductCatalog maintenanceProductCatalog;

	public AvoskaServiceLocalAsyncImpl() {

	}
	
	@Override
	public void getProducts(AsyncCallback<List<Product>> callback) {
		try {
			callback.onSuccess(maintenanceProductCatalog.getProducts());
		} catch (AvoskaModelException e) {
			callback.onFailure(e);
		}
		
	}

	@Override
	public void createProduct(String name, AsyncCallback<Product> callback) {
		try {
			callback.onSuccess(maintenanceProductCatalog.createProduct(name));
		} catch (AvoskaModelException e) {
			callback.onFailure(e);
		}
	}

	@Override
	public void renameProduct(Product product, String newName,
			AsyncCallback<Product> callback) {
		try {
			callback.onSuccess(maintenanceProductCatalog.renameProduct(product, newName));
		} catch (AvoskaModelException e) {
			callback.onFailure(e);
		}

	}

	@Override
	public void removeProduct(Product product, AsyncCallback<Boolean> callback) {
		try {
			callback.onSuccess(maintenanceProductCatalog.removeProduct(product));
		} catch (AvoskaModelException e) {
			callback.onFailure(e);
		}
	}

	@Override
	public void switchNeedPurchase(Product product, Boolean newNeedPurchase,
			AsyncCallback<Product> callback) {
		callback.onSuccess(maintenanceProductCatalog.switchNeedPurchase(product, newNeedPurchase));
	}

	public MaintenanceProductCatalog getMaintenanceProductCatalog() {
		return maintenanceProductCatalog;
	}

	public void setMaintenanceProductCatalog(MaintenanceProductCatalog catalog) {
		this.maintenanceProductCatalog = catalog;
	}
}
